﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Configuration
{
    public class PlayerConfiguration : ConfigurationSection
    {
        private static PlayerConfiguration instance = null;
        private static readonly object mylock = new object();

        private PlayerConfiguration() { }

        public static PlayerConfiguration GetInstance()
        {
            lock ((mylock))
            {
                if (instance == null)
                {
                    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
                    configFileMap.ExeConfigFilename = "Configuration/Game.config";

                    instance = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None)
                                    .GetSection("PlayerConfiguration") as PlayerConfiguration;
                }
                return instance;
            }
        }

        [ConfigurationProperty("PlayerProfilesList")]
        public PlayerProfilesList PlayerProfilesList
        {
            get
            {
                return base["PlayerProfilesList"] as PlayerProfilesList;
            }
        }
    }

    [ConfigurationCollection(typeof(PlayerProfile), AddItemName = "PlayerProfile", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class PlayerProfilesList : ConfigurationElementCollection
    {
        private List<PlayerProfile> _PlayerProfile;
        public List<PlayerProfile> PlayerProfile
        {
            get
            {
                if (_PlayerProfile == null)
                {
                    _PlayerProfile = new List<PlayerProfile>();
                    foreach (PlayerProfile profile in this)
                    {
                        _PlayerProfile.Add(profile);
                    }
                }
                return _PlayerProfile;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new PlayerProfile();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return element as PlayerProfile;
        }
    }

    public class PlayerProfile : ConfigurationElement
    {
        [ConfigurationProperty("ID", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string ID
        {
            get
            {
                return base["ID"] as string;
            }
            set
            {
                base["ID"] = value;
            }
        }

        [ConfigurationProperty("Name", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string Name
        {
            get
            {
                return base["Name"] as string;
            }
            set
            {
                base["Name"] = value;
            }
        }

        [ConfigurationProperty("Color", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string Color
        {
            get
            {
                return base["Color"] as string;
            }
            set
            {
                base["Color"] = value;
            }
        }

        [ConfigurationProperty("SystemConfigID", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string SystemConfigID
        {
            get
            {
                return base["SystemConfigID"] as string;
            }
            set
            {
                base["SystemConfigID"] = value;
            }
        }

        [ConfigurationProperty("NetWorkConfigID", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string NetWorkConfigID
        {
            get
            {
                return base["NetWorkConfigID"] as string;
            }
            set
            {
                base["NetWorkConfigID"] = value;
            }
        }

        [ConfigurationProperty("ActiveProfile", DefaultValue = false, IsKey = false, IsRequired = true)]
        public bool ActiveProfile
        {
            get
            {
                return Convert.ToBoolean(base["ActiveProfile"]);
            }
            set
            {
                base["ActiveProfile"] = value;
            }
        }
    }
}
